/*!
    \file    main.c
    \brief   USART transmit and receive interrupt

    \version 2017-02-10, V1.0.0, firmware for GD32F30x
    \version 2018-10-10, V1.1.0, firmware for GD32F30x
    \version 2018-12-25, V2.0.0, firmware for GD32F30x
    \version 2020-09-30, V2.1.0, firmware for GD32F30x
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/

#include "gd32f30x.h"
#include "board.h"
#include "gd32f30x_it.h"
#include <stdio.h>
#include <string.h>

#include "los_config.h"
#include "los_debug.h"
#include "los_interrupt.h"
#include "los_task.h"
#include "los_tick.h"

static char rvBuf[256];
static int rvRd;

#if defined (__GNUC__) && !defined (__clang__)
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

/**
  * @brief  retargets the c library printf function to the usart.
  * @param  none
  * @retval none
  */
PUTCHAR_PROTOTYPE
{
    while(usart_flag_get(PRINT_UART, USART_FLAG_TBE) == RESET);
    usart_data_transmit(PRINT_UART, (uint16_t)ch);

  return ch;
}

//_write函數在syscalls.c中， 使用__weak定義， 所以可以直接在其他文件中定義_write函數
int _write(int file, char *ptr, int len)
{
    int i;
    for (i = 0; (i < len) && (*ptr != 0); i++)
    {
        __io_putchar(*ptr++);
    }
    return len;
}

static void uart_print_init(void)
{
    /* enable GPIO clock */
    rcu_periph_clock_enable(PRINT_UART_TX_CLK);
    rcu_periph_clock_enable(PRINT_UART_RX_CLK);

    /* connect port to USARTx_Tx */
    gpio_init(PRINT_UART_TX_PORT, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, PRINT_UART_TX_PIN);

    /* connect port to USARTx_Rx */
    gpio_init(PRINT_UART_RX_PORT, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, PRINT_UART_RX_PIN);

    /* enable USART clock */
    rcu_periph_clock_enable(PRINT_UART_CLK);

    /* USART configure */
    usart_deinit(PRINT_UART);
    usart_baudrate_set(PRINT_UART, 115200);
    usart_parity_config(PRINT_UART, USART_PM_NONE);
    usart_word_length_set(PRINT_UART, USART_WL_8BIT);
    usart_stop_bit_set(PRINT_UART, USART_STB_1BIT);
    usart_receive_config(PRINT_UART, USART_RECEIVE_ENABLE);
    usart_transmit_config(PRINT_UART, USART_TRANSMIT_ENABLE);
    usart_enable(PRINT_UART);
}

VOID TaskSampleEntry2(VOID)
{
    printf("Task2 Entry\n");
    while (1) {
        GPIO_BC(GPIOD) = GPIO_PIN_6;
        LOS_TaskDelay(3000); /* 10 Seconds */
        GPIO_BOP(GPIOD) = GPIO_PIN_6;
        LOS_TaskDelay(3000); /* 10 Seconds */
    }
}

VOID TaskSampleEntry1(VOID)
{
    printf("Task1 Entry\n");
    while (1) {
        GPIO_BC(GPIOD) = GPIO_PIN_4;
        LOS_TaskDelay(500); /* 1 Seconds */
        GPIO_BOP(GPIOD) = GPIO_PIN_4;
        LOS_TaskDelay(500); /* 1 Seconds */
    }
}

VOID TaskSample(VOID)
{
    UINT32 uwRet;
    UINT32 taskID1;
    UINT32 taskID2;
    TSK_INIT_PARAM_S stTask = {0};

    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry1;
    stTask.uwStackSize = 0x1000;
    stTask.pcName = "Task1";
    stTask.usTaskPrio = 6; /* Os task priority is 6 */
    uwRet = LOS_TaskCreate(&taskID1, &stTask);
    if (uwRet == LOS_OK) {
        printf("Task1 create ok\n");
    }

    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry2;
    stTask.uwStackSize = 0x1000;
    stTask.pcName = "Task2";
    stTask.usTaskPrio = 7; /* Os task priority is 7 */
    uwRet = LOS_TaskCreate(&taskID2, &stTask);
    if (uwRet == LOS_OK) {
        printf("Task2 create ok\n");
    }
}

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    uint32_t tick;
    int ret;
    /* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOD);

    uart_print_init();

    gpio_init(GPIOD, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
    gpio_init(GPIOD, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
    gpio_init(GPIOD, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    GPIO_BOP(GPIOD) = GPIO_PIN_4;
    GPIO_BOP(GPIOD) = GPIO_PIN_5;
    GPIO_BOP(GPIOD) = GPIO_PIN_6;

    ret = LOS_KernelInit();

    if (ret == LOS_OK) {
        GPIO_BC(GPIOD) = GPIO_PIN_5;
        TaskSample();
        LOS_Start();
    }

    while (1) {}    // do nothing
}
